import { ValidationError } from "express-validator"
import { CustomError } from "./custom-error"

export class RequestValidationError extends CustomError {
	statusCode = 400;
	public errors: ValidationError[];
	constructor(public errorsList: ValidationError[]) {
		super("身份验证失败");
		this.errors = errorsList;
		Object.setPrototypeOf(this, RequestValidationError.prototype);
	}
	serializeErrors() {
		return this.errors.map(err => {
			if (err.type === "field") return {message: err.msg, field: err.path};
			return {message: err.msg };
		});
	}
}
